﻿using System;
using System.Diagnostics;
using System.IO;

namespace SfsTools.Core
{
    /// <summary>
    /// Base class for directory entries.
    /// </summary>
    internal abstract class BaseDirectoryEntry : FileSystemObjectEntry
    {
        #region Properties

        /// <summary>
        /// Gets or sets the name of the directory.
        /// </summary>
        /// <exception cref="ArgumentNullException">The argument <paramref name="value"/> is <see langword="null"/>.</exception>
        public Byte[] Name
        {
            [DebuggerStepThrough]
            get
            {
                byte[] buffer = new byte[54];
                Array.Copy(FBuffer, 0x0A, buffer, 0, 54);
                return buffer;
            }
            [DebuggerStepThrough]
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");
                Array.Copy(value, 0, FBuffer, 0x0A, Math.Min(54, value.Length));
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Copies the contents of this entry to <paramref name="buffer"/>.
        /// </summary>
        /// <param name="buffer">The buffer to copy the contents to.</param>
        /// <param name="index">The starting byte index.</param>
        [DebuggerStepThrough]
        public override void CopyTo(byte[] buffer, int index)
        {
            Array.Copy(FBuffer, 0x0A, buffer, index, 54);
        }

        /// <summary>
        /// Copies the contents of <paramref name="buffer"/> to this entry.
        /// </summary>
        /// <param name="buffer">The buffer to copy the contents from.</param>
        /// <param name="index">The starting byte index.</param>
        [DebuggerStepThrough]
        public override void CopyFrom(byte[] buffer, int index)
        {
            Array.Copy(buffer, index, FBuffer, 0x0A, Math.Min(54, buffer.Length - index));
        }

        #endregion

        #region Constructors
        /// <overloads>Initializes a new instance of <see cref="BaseDirectoryEntry"/>.</overloads>
        /// <summary>
        /// Initializes a new instance of <see cref="BaseDirectoryEntry"/>.
        /// </summary>
        /// <param name="position">The position of this entry on the volume.</param>
        /// <param name="buffer">The buffer.</param>
        [DebuggerStepThrough]
        public BaseDirectoryEntry(Int64 position, byte[] buffer)
            : base(position, buffer)
        {
        }
        /// <summary>
        /// Initializes a new instance of <see cref="BaseDirectoryEntry"/> from an existing instace.
        /// </summary>
        /// <param name="value">The instance to copy.</param>
        /// <exception cref="ArgumentNullException">The argument <paramref name="value"/> is <see langword="null"/>.</exception>
        [DebuggerStepThrough]
        public BaseDirectoryEntry(BaseDirectoryEntry value)
            : base(value)
        {
        }
        #endregion
    }
}
